<template>
  <div class="classroom-container">
    <!-- 顶部搜索和筛选 -->
    <div class="classroom-header">
      <div class="header-content">
        <div class="search-section">
          <h1>智能课堂</h1>
          <p>发现优质课程，开启学习之旅</p>
          <div class="search-bar">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索课程、讲师、技能..."
              size="large"
              clearable
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
              <template #append>
                <el-button type="primary" @click="handleSearch">
                  搜索
                </el-button>
              </template>
            </el-input>
          </div>
        </div>
        
        <div class="stats-section">
          <div class="stat-item">
            <div class="stat-number">8,500+</div>
            <div class="stat-label">优质课程</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">500+</div>
            <div class="stat-label">专业讲师</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">125K+</div>
            <div class="stat-label">学习用户</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 筛选和排序 -->
    <div class="filter-section">
      <div class="filter-tabs">
        <div 
          v-for="category in categories" 
          :key="category.key"
          class="filter-tab"
          :class="{ active: activeCategory === category.key }"
          @click="activeCategory = category.key"
        >
          <el-icon><component :is="category.icon" /></el-icon>
          {{ category.label }}
        </div>
      </div>
      
      <div class="filter-controls">
        <el-select v-model="sortBy" placeholder="排序方式" style="width: 140px">
          <el-option label="最新发布" value="latest" />
          <el-option label="最受欢迎" value="popular" />
          <el-option label="评分最高" value="rating" />
          <el-option label="价格最低" value="price" />
        </el-select>
        
        <el-select v-model="levelFilter" placeholder="难度等级" style="width: 120px">
          <el-option label="全部等级" value="" />
          <el-option label="入门" value="beginner" />
          <el-option label="进阶" value="intermediate" />
          <el-option label="高级" value="advanced" />
        </el-select>
        
        <el-select v-model="priceFilter" placeholder="价格筛选" style="width: 120px">
          <el-option label="全部价格" value="" />
          <el-option label="免费" value="free" />
          <el-option label="付费" value="paid" />
        </el-select>
      </div>
    </div>

    <!-- 推荐课程轮播 -->
    <div class="featured-section" v-if="activeCategory === 'all'">
      <h2>精选推荐</h2>
      <el-carousel height="300px" indicator-position="outside">
        <el-carousel-item v-for="course in featuredCourses" :key="course.id">
          <div class="featured-course" @click="viewCourse(course)">
            <div class="course-image">
              <img :src="course.cover" :alt="course.title" />
              <div class="course-overlay">
                <el-button type="primary" size="large">
                  <el-icon><VideoPlay /></el-icon>
                  立即学习
                </el-button>
              </div>
            </div>
            <div class="course-info">
              <h3>{{ course.title }}</h3>
              <p>{{ course.description }}</p>
              <div class="course-meta">
                <span class="instructor">{{ course.instructor }}</span>
                <span class="students">{{ course.studentCount }}人学习</span>
                <el-rate v-model="course.rating" disabled show-score />
              </div>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 课程列表 -->
    <div class="courses-section">
      <div class="section-header">
        <h2>{{ getCategoryTitle() }}</h2>
        <div class="view-toggle">
          <el-button-group>
            <el-button 
              :type="viewMode === 'grid' ? 'primary' : ''"
              @click="viewMode = 'grid'"
            >
              <el-icon><Grid /></el-icon>
            </el-button>
            <el-button 
              :type="viewMode === 'list' ? 'primary' : ''"
              @click="viewMode = 'list'"
            >
              <el-icon><List /></el-icon>
            </el-button>
          </el-button-group>
        </div>
      </div>

      <!-- 网格视图 -->
      <div v-if="viewMode === 'grid'" class="courses-grid">
        <div 
          v-for="course in filteredCourses" 
          :key="course.id"
          class="course-card"
          @click="viewCourse(course)"
        >
          <div class="course-image">
            <img :src="course.cover" :alt="course.title" />
            <div class="course-badge" v-if="course.isNew">新课</div>
            <div class="course-level">{{ getLevelText(course.level) }}</div>
            <div class="course-duration">{{ course.duration }}</div>
          </div>
          
          <div class="course-content">
            <h3>{{ course.title }}</h3>
            <p class="course-description">{{ course.description }}</p>
            
            <div class="course-instructor">
              <el-avatar :size="24" :src="course.instructorAvatar">
                {{ course.instructor.charAt(0) }}
              </el-avatar>
              <span>{{ course.instructor }}</span>
            </div>
            
            <div class="course-stats">
              <div class="rating">
                <el-rate v-model="course.rating" disabled size="small" />
                <span class="rating-text">{{ course.rating }}</span>
              </div>
              <div class="students">
                <el-icon><User /></el-icon>
                {{ formatNumber(course.studentCount) }}
              </div>
            </div>
            
            <div class="course-footer">
              <div class="course-price">
                <span v-if="course.price === 0" class="free">免费</span>
                <span v-else class="price">¥{{ course.price }}</span>
              </div>
              <el-button type="primary" size="small">
                {{ course.price === 0 ? '开始学习' : '立即购买' }}
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 列表视图 -->
      <div v-else class="courses-list">
        <div 
          v-for="course in filteredCourses" 
          :key="course.id"
          class="course-list-item"
          @click="viewCourse(course)"
        >
          <div class="course-image">
            <img :src="course.cover" :alt="course.title" />
          </div>
          
          <div class="course-content">
            <div class="course-header">
              <h3>{{ course.title }}</h3>
              <div class="course-badges">
                <el-tag v-if="course.isNew" type="success" size="small">新课</el-tag>
                <el-tag :type="getLevelType(course.level)" size="small">
                  {{ getLevelText(course.level) }}
                </el-tag>
              </div>
            </div>
            
            <p class="course-description">{{ course.description }}</p>
            
            <div class="course-meta">
              <div class="instructor">
                <el-avatar :size="20" :src="course.instructorAvatar">
                  {{ course.instructor.charAt(0) }}
                </el-avatar>
                <span>{{ course.instructor }}</span>
              </div>
              
              <div class="stats">
                <span class="duration">{{ course.duration }}</span>
                <span class="students">{{ formatNumber(course.studentCount) }}人学习</span>
                <div class="rating">
                  <el-rate v-model="course.rating" disabled size="small" />
                  <span>{{ course.rating }}</span>
                </div>
              </div>
            </div>
          </div>
          
          <div class="course-actions">
            <div class="course-price">
              <span v-if="course.price === 0" class="free">免费</span>
              <span v-else class="price">¥{{ course.price }}</span>
            </div>
            <el-button type="primary">
              {{ course.price === 0 ? '开始学习' : '立即购买' }}
            </el-button>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[12, 24, 48]"
          :total="totalCourses"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Search, VideoPlay, Grid, List, User,
  Monitor, Document, Reading, Trophy, Star
} from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const searchKeyword = ref('')
const activeCategory = ref('all')
const sortBy = ref('latest')
const levelFilter = ref('')
const priceFilter = ref('')
const viewMode = ref('grid')
const currentPage = ref(1)
const pageSize = ref(12)
const totalCourses = ref(156)

// 分类配置
const categories = ref([
  { key: 'all', label: '全部课程', icon: 'Grid' },
  { key: 'frontend', label: '前端开发', icon: 'Monitor' },
  { key: 'backend', label: '后端开发', icon: 'Document' },
  { key: 'mobile', label: '移动开发', icon: 'Reading' },
  { key: 'ai', label: '人工智能', icon: 'Trophy' },
  { key: 'design', label: '设计', icon: 'Star' }
])

// 精选课程
const featuredCourses = ref([
  {
    id: 1,
    title: 'Vue3 + TypeScript 企业级项目实战',
    description: '从零开始构建现代化前端应用，掌握Vue3生态系统',
    cover: '/images/featured-1.jpg',
    instructor: '张老师',
    studentCount: 12580,
    rating: 4.9
  },
  {
    id: 2,
    title: 'Node.js 全栈开发实战',
    description: '掌握服务端JavaScript开发，构建高性能Web应用',
    cover: '/images/featured-2.jpg',
    instructor: '李老师',
    studentCount: 8960,
    rating: 4.8
  }
])

// 课程列表
const courses = ref([
  {
    id: 1,
    title: 'Vue3 + TypeScript 实战开发',
    description: '从基础到进阶，全面掌握Vue3和TypeScript开发技能',
    cover: '/images/course-1.jpg',
    instructor: '张老师',
    instructorAvatar: '/images/instructor-1.jpg',
    category: 'frontend',
    level: 'intermediate',
    duration: '24小时',
    studentCount: 12580,
    rating: 4.9,
    price: 199,
    isNew: true
  },
  {
    id: 2,
    title: 'React 企业级应用开发',
    description: '学习React生态系统，构建可维护的大型应用',
    cover: '/images/course-2.jpg',
    instructor: '李老师',
    instructorAvatar: '/images/instructor-2.jpg',
    category: 'frontend',
    level: 'advanced',
    duration: '32小时',
    studentCount: 8960,
    rating: 4.8,
    price: 299,
    isNew: false
  },
  {
    id: 3,
    title: 'JavaScript 基础入门',
    description: '零基础学习JavaScript，为前端开发打下坚实基础',
    cover: '/images/course-3.jpg',
    instructor: '王老师',
    instructorAvatar: '/images/instructor-3.jpg',
    category: 'frontend',
    level: 'beginner',
    duration: '16小时',
    studentCount: 25600,
    rating: 4.7,
    price: 0,
    isNew: false
  }
  // 更多课程数据...
])

// 计算属性
const filteredCourses = computed(() => {
  let filtered = courses.value

  // 分类筛选
  if (activeCategory.value !== 'all') {
    filtered = filtered.filter(course => course.category === activeCategory.value)
  }

  // 难度筛选
  if (levelFilter.value) {
    filtered = filtered.filter(course => course.level === levelFilter.value)
  }

  // 价格筛选
  if (priceFilter.value === 'free') {
    filtered = filtered.filter(course => course.price === 0)
  } else if (priceFilter.value === 'paid') {
    filtered = filtered.filter(course => course.price > 0)
  }

  // 搜索筛选
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    filtered = filtered.filter(course => 
      course.title.toLowerCase().includes(keyword) ||
      course.description.toLowerCase().includes(keyword) ||
      course.instructor.toLowerCase().includes(keyword)
    )
  }

  // 排序
  switch (sortBy.value) {
    case 'popular':
      filtered.sort((a, b) => b.studentCount - a.studentCount)
      break
    case 'rating':
      filtered.sort((a, b) => b.rating - a.rating)
      break
    case 'price':
      filtered.sort((a, b) => a.price - b.price)
      break
    default: // latest
      filtered.sort((a, b) => b.isNew - a.isNew)
  }

  return filtered
})

// 方法
const getCategoryTitle = () => {
  const category = categories.value.find(c => c.key === activeCategory.value)
  return category ? category.label : '全部课程'
}

const getLevelText = (level) => {
  const levelMap = {
    beginner: '入门',
    intermediate: '进阶',
    advanced: '高级'
  }
  return levelMap[level] || level
}

const getLevelType = (level) => {
  const typeMap = {
    beginner: 'success',
    intermediate: 'warning',
    advanced: 'danger'
  }
  return typeMap[level] || ''
}

const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toLocaleString()
}

const handleSearch = () => {
  currentPage.value = 1
  // 执行搜索逻辑
}

const viewCourse = (course) => {
  router.push(`/classroom/course/${course.id}`)
}

const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
}

const handleCurrentChange = (page) => {
  currentPage.value = page
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.classroom-container {
  min-height: 100vh;
  background: var(--el-bg-color-page);
}

.classroom-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 60px;
  align-items: center;
  
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
    gap: 30px;
  }
}

.search-section {
  h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 16px;
  }
  
  p {
    font-size: 1.25rem;
    opacity: 0.9;
    margin-bottom: 32px;
  }
}

.search-bar {
  max-width: 500px;
  
  .el-input {
    :deep(.el-input__wrapper) {
      background: rgba(255, 255, 255, 0.95);
      border: none;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    }
  }
}

.stats-section {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.stat-item {
  text-align: center;
  
  .stat-number {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 8px;
  }
  
  .stat-label {
    font-size: 1rem;
    opacity: 0.8;
  }
}

.filter-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 20px;
  }
}

.filter-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.filter-tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
  border: 1px solid var(--el-border-color-lighter);
  
  &:hover {
    border-color: var(--el-color-primary);
    color: var(--el-color-primary);
  }
  
  &.active {
    background: var(--el-color-primary);
    color: white;
    border-color: var(--el-color-primary);
  }
}

.filter-controls {
  display: flex;
  gap: 12px;
  
  @media (max-width: 768px) {
    flex-wrap: wrap;
  }
}

.featured-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 40px;
  
  h2 {
    font-size: 2rem;
    font-weight: 600;
    margin-bottom: 24px;
    color: var(--el-text-color-primary);
  }
}

.featured-course {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  align-items: center;
  cursor: pointer;
  
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
  }
}

.course-image {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .course-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
  }
  
  &:hover .course-overlay {
    opacity: 1;
  }
}

.courses-section {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 40px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  
  h2 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
}

.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 40px;
}

.course-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  cursor: pointer;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
  
  .course-image {
    position: relative;
    height: 180px;
    
    .course-badge {
      position: absolute;
      top: 12px;
      left: 12px;
      background: var(--el-color-success);
      color: white;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
    }
    
    .course-level {
      position: absolute;
      top: 12px;
      right: 12px;
      background: rgba(0, 0, 0, 0.7);
      color: white;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
    }
    
    .course-duration {
      position: absolute;
      bottom: 12px;
      right: 12px;
      background: rgba(0, 0, 0, 0.7);
      color: white;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
    }
  }
}

.course-content {
  padding: 20px;
  
  h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 8px;
    @include text-ellipsis(2);
  }
  
  .course-description {
    font-size: 14px;
    color: var(--el-text-color-secondary);
    margin-bottom: 16px;
    @include text-ellipsis(2);
  }
}

.course-instructor {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: 14px;
  color: var(--el-text-color-regular);
}

.course-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  
  .rating {
    display: flex;
    align-items: center;
    gap: 4px;
    
    .rating-text {
      font-size: 14px;
      color: var(--el-text-color-secondary);
    }
  }
  
  .students {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.course-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .course-price {
    .free {
      color: var(--el-color-success);
      font-weight: 600;
    }
    
    .price {
      color: var(--el-color-danger);
      font-weight: 600;
      font-size: 18px;
    }
  }
}

.courses-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-bottom: 40px;
}

.course-list-item {
  display: flex;
  gap: 20px;
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
  cursor: pointer;
  
  &:hover {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  }
  
  .course-image {
    width: 200px;
    height: 120px;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
  }
  
  .course-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  
  .course-actions {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
    gap: 12px;
  }
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}
</style>
